package com.citi.gts.galto.modules.validation.rule.impl;

import com.citi.gts.galto.core.tools.exception.NonExistenceException;
import com.citi.gts.galto.core.tools.exception.ReferenceException;
import com.citi.gts.galto.core.tools.reference.Field;
import com.citi.gts.galto.modules.validation.Precondition;
import com.citi.gts.galto.modules.validation.dao.BranchDao;
import com.citi.gts.galto.modules.validation.exception.SubServiceException;
import com.citi.gts.galto.modules.validation.exception.ValidationException;
import com.citi.gts.galto.modules.validation.vo.SampleInstruction;
import org.springframework.beans.factory.annotation.Required;

import java.util.HashSet;
import java.util.Set;

public class BranchExistenceRule extends LeafRule {

    private BranchDao branchDao;


    @Override
    @Precondition(rule = {"branchInformedRule", "countryExistenceRule"}, currentRule = "branchExistenceRule")
    public void validate(SampleInstruction instruction) throws SubServiceException, ValidationException {

        System.out.println("Within " + getClass().getSimpleName());
        try {

            branchDao.exist(instruction.getOrigCountryCode(), instruction.getBeneBranchCode());

        } catch (NonExistenceException e) {

            throw new SubServiceException(repair, e);

        }

    }

    @Required
    public void setBranchDao(BranchDao branchDao) {
        this.branchDao = branchDao;
    }

    @Override
    public void setFieldsValidated() throws ReferenceException {

        Set<Field> fieldValidatedSet = new HashSet<Field>(2);
        fieldValidatedSet.add(fieldReference.find("beneBranchCode"));
        fieldValidatedSet.add(fieldReference.find("origCountryCode"));
        repair.setFieldSet(fieldValidatedSet);
    }
}
